﻿using System;
using System.Windows;
using CredentialsManager.ViewModels;
using CredentialsManager.Views;

namespace CredentialsManager
{
    public class ViewDialogService : IDialogService
    {
        #region Public methods

        public void Show(ViewModelBase viewModel)
        {
            var view = SetupView(viewModel);
            view.Show();
        }

        public bool? ShowModal(ViewModelBase viewModel)
        {
            var view = SetupView(viewModel);
            return view.ShowDialog();
        }

        #endregion

        #region Private methods

        private Window SetupView(ViewModelBase viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }

            var view = new DialogView();

            EventHandler handler = null;
            handler = delegate
            {
                viewModel.RequestClose -= handler;
                view.Close();
            };
            viewModel.RequestClose += handler;

            view.DataContext = viewModel;
            return view;
        }

        #endregion
    }
}
